package com.hqumath.androidnative;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;

import androidx.appcompat.app.AppCompatActivity;

import com.hqumath.androidnative.app.AppExecutors;
import com.hqumath.androidnative.databinding.ActivityMainBinding;
import com.hqumath.androidnative.tools.GameDevice;
import com.hqumath.androidnative.utils.CommonUtil;

import org.freedesktop.forcefeedback.ForceFeedback;
import org.freedesktop.test.Test;

/**
 * ****************************************************************
 * 作    者: Created by gyd
 * 创建时间: 2023/9/5 16:24
 * 文件描述:
 * 注意事项:
 * ****************************************************************
 */
public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;

    private ForceFeedback forceFeedback;
    private float autoCenter = 1f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        CommonUtil.init(this);

        //音频解码器
        //AudioCodec audioEncode = new AudioCodec();
        /*byte[] data = new byte[240];//每次收到的数据240B
        byte[] encodeData = new byte[240];//编码后的数据240B
        byte[] decodeData = new byte[1600];//解码后大小480B

        int encodeLen = AudioCodec.g711Encode(data, encodeData, data.length, 1);
        binding.tvTest.setText("encoder=" + encodeLen);

        int decodeLen = AudioCodec.g711Decode(data, decodeData, data.length, 1);
        binding.btnTest.setText("decoder=" + decodeLen);

        encodeLen = AudioCodec.g723Encode(data, encodeData, data.length);
        binding.tvTest.setText("encoder=" + encodeLen);

        encodeLen = AudioCodec.g723Decode(data, decodeData, data.length);
        binding.btnTest.setText("decoder=" + encodeLen);*/

        //力反馈
        binding.btnTest.setOnClickListener(v -> {
            if (forceFeedback != null) {
                //回中力
                autoCenter = autoCenter - 0.1f;
                if (autoCenter <= -0.000001f) {
                    autoCenter = 1f;
                }
                int result = forceFeedback.setAutoCenter(autoCenter);

//                forceFeedback.setAutoCenter(0f);
//                forceFeedback.setConstant(0.5f);
                //forceFeedback.queryDeviceCapabilities();
            }
        });
        forceFeedback = ForceFeedback.getInstance();
        findAndConnectWheel();//查找并连接设备


        //测试
        /*Test test = new Test();
        binding.tvTest.setText(test.operateString1("sss"));

        test.thread1();*/
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (forceFeedback != null) {
            forceFeedback.disconnect();
            forceFeedback = null;
        }
        CommonUtil.killProgress();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (GameDevice.onKeyDown(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (GameDevice.onKeyUp(keyCode, event)) {
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (GameDevice.onGenericMotionEvent(event)) {
            return true;
        }
        return super.onGenericMotionEvent(event);
    }

    //查找并连接设备
    private void findAndConnectWheel() {
        AppExecutors.getInstance().workThread().execute(() -> {
            if (forceFeedback != null) {
                forceFeedback.findAndConnect();
                GameDevice.curControllerId = forceFeedback.getDeviceId();
            }
        });
    }
}
